{
 "cells": [
  {
   "cell_type": "raw",
   "metadata": {},
   "source": [
    "基于矩阵分解的协同过滤"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import sys\n",
    "import imp\n",
    "imp.reload(sys)\n",
    "\n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "\n",
    "import pickle\n",
    "import scipy.io as sio\n",
    "import os\n",
    "import scipy.spatial.distance as ssd\n",
    "from numpy.random import random"
   ]
  },
  {
   "cell_type": "raw",
   "metadata": {},
   "source": [
    "初始化"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "user_index = pickle.load(open(\"user_index.pkl\",'rb'))\n",
    "item_index = pickle.load(open(\"item_index.pkl\",'rb'))\n",
    "\n",
    "n_users = len(user_index)\n",
    "n_items = len(item_index)\n",
    "\n",
    "user_items = pickle.load(open(\"user_items.pkl\",'rb'))\n",
    "item_users = pickle.load(open(\"item_users.pkl\",'rb'))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "train = pd.read_csv('E:/csdn/week5/triplet_dataset_sub_song_merged.csv')\n",
    "triplet_train_sum_df = train[['user','listen_count']].groupby('user').sum().reset_index()\n",
    "triplet_train_sum_df.rename(columns = {'listen_count':'total_listen_count'},inplace = True)\n",
    "train = pd.merge(train,triplet_train_sum_df)\n",
    "train['fractional_play_count'] = train['listen_count']/train['total_listen_count']\n",
    "del triplet_train_sum_df\n",
    "\n",
    "song_count_df = pd.read_csv('E:/csdn/week5/song_playcount_df.csv')\n",
    "song_count_subset = song_count_df.head(n=1000)\n",
    "song_subset = list(song_count_subset.song)\n",
    "train_sub = train[train.song.isin(song_subset)]\n",
    "del train"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "K = 20"
   ]
  },
  {
   "cell_type": "raw",
   "metadata": {},
   "source": [
    "初始化参数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "bi = np.zeros(n_items)\n",
    "bu = np.zeros(n_users)\n",
    "\n",
    "P = random((n_users,K))/10*(np.sqrt(K))\n",
    "Q = random((K,n_items))/10*(np.sqrt(K))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "n_samples = train_sub.shape[0]\n",
    "\n",
    "mu = 0.0\n",
    "uids = []\n",
    "i_ids = []\n",
    "\n",
    "R = np.matrix(np.zeros(shape = (n_users,n_items)),float)\n",
    "\n",
    "for i in range(n_samples):\n",
    "    uid = user_index[train_sub.iloc[i]['user']]\n",
    "    iid = item_index[train_sub.iloc[i]['song']]\n",
    "    \n",
    "    uids.append(uid)\n",
    "    i_ids.append(iid)\n",
    "    \n",
    "    R[uid,iid] = train_sub.iloc[i]['fractional_play_count']\n",
    "    mu += R[uid,iid]\n",
    "    \n",
    "mu /= n_samples"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "def pred_SVD(uid,i_id):\n",
    "    score = mu + bi[i_id] + bu[uid] + np.dot(P[uid,:],Q[:,i_id])\n",
    "    return score"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "the 0 -th step is running\n",
      "the rmse of the 0 th step on train data is :2150.662261689261\n",
      "the 1 -th step is running\n",
      "the rmse of the 1 th step on train data is :127.65762349426662\n",
      "the 2 -th step is running\n",
      "the rmse of the 2 th step on train data is :121.32761574588427\n",
      "the 3 -th step is running\n",
      "the rmse of the 3 th step on train data is :118.50985946574444\n",
      "the 4 -th step is running\n",
      "the rmse of the 4 th step on train data is :116.6672970312518\n",
      "the 5 -th step is running\n",
      "the rmse of the 5 th step on train data is :115.28608333023848\n",
      "the 6 -th step is running\n",
      "the rmse of the 6 th step on train data is :114.28816691159943\n",
      "the 7 -th step is running\n",
      "the rmse of the 7 th step on train data is :113.42545025587624\n",
      "the 8 -th step is running\n",
      "the rmse of the 8 th step on train data is :112.66233646908628\n",
      "the 9 -th step is running\n",
      "the rmse of the 9 th step on train data is :112.06516597501515\n",
      "the 10 -th step is running\n",
      "the rmse of the 10 th step on train data is :111.6280541098624\n",
      "the 11 -th step is running\n",
      "the rmse of the 11 th step on train data is :111.05893382523175\n",
      "the 12 -th step is running\n",
      "the rmse of the 12 th step on train data is :110.71727642495262\n",
      "the 13 -th step is running\n",
      "the rmse of the 13 th step on train data is :110.35138626264242\n",
      "the 14 -th step is running\n",
      "the rmse of the 14 th step on train data is :109.96771301545472\n",
      "the 15 -th step is running\n",
      "the rmse of the 15 th step on train data is :109.6849682469091\n",
      "the 16 -th step is running\n",
      "the rmse of the 16 th step on train data is :109.4129470354293\n",
      "the 17 -th step is running\n",
      "the rmse of the 17 th step on train data is :109.1315882311303\n",
      "the 18 -th step is running\n",
      "the rmse of the 18 th step on train data is :108.9591668717006\n",
      "the 19 -th step is running\n",
      "the rmse of the 19 th step on train data is :108.71311096789171\n",
      "the 20 -th step is running\n",
      "the rmse of the 20 th step on train data is :108.55153207839683\n",
      "the 21 -th step is running\n",
      "the rmse of the 21 th step on train data is :108.3629608185032\n",
      "the 22 -th step is running\n",
      "the rmse of the 22 th step on train data is :108.1778446708278\n",
      "the 23 -th step is running\n",
      "the rmse of the 23 th step on train data is :108.06292299867853\n",
      "the 24 -th step is running\n",
      "the rmse of the 24 th step on train data is :107.88710570602068\n",
      "the 25 -th step is running\n",
      "the rmse of the 25 th step on train data is :107.76672714526045\n",
      "the 26 -th step is running\n",
      "the rmse of the 26 th step on train data is :107.6774343246375\n",
      "the 27 -th step is running\n",
      "the rmse of the 27 th step on train data is :107.54857283334393\n",
      "the 28 -th step is running\n",
      "the rmse of the 28 th step on train data is :107.44599477286306\n",
      "the 29 -th step is running\n",
      "the rmse of the 29 th step on train data is :107.36359997792671\n",
      "the 30 -th step is running\n",
      "the rmse of the 30 th step on train data is :107.27172944687118\n",
      "the 31 -th step is running\n",
      "the rmse of the 31 th step on train data is :107.19065264499336\n",
      "the 32 -th step is running\n",
      "the rmse of the 32 th step on train data is :107.1099966565433\n",
      "the 33 -th step is running\n",
      "the rmse of the 33 th step on train data is :107.03483043266434\n",
      "the 34 -th step is running\n",
      "the rmse of the 34 th step on train data is :106.98920263394382\n",
      "the 35 -th step is running\n",
      "the rmse of the 35 th step on train data is :106.90713683464739\n",
      "the 36 -th step is running\n",
      "the rmse of the 36 th step on train data is :106.85849911728899\n",
      "the 37 -th step is running\n",
      "the rmse of the 37 th step on train data is :106.79763555650128\n",
      "the 38 -th step is running\n",
      "the rmse of the 38 th step on train data is :106.75605027099034\n",
      "the 39 -th step is running\n",
      "the rmse of the 39 th step on train data is :106.7107194688025\n",
      "the 40 -th step is running\n",
      "the rmse of the 40 th step on train data is :106.65310902724296\n",
      "the 41 -th step is running\n",
      "the rmse of the 41 th step on train data is :106.63184666445247\n",
      "the 42 -th step is running\n",
      "the rmse of the 42 th step on train data is :106.58894211998091\n",
      "the 43 -th step is running\n",
      "the rmse of the 43 th step on train data is :106.55339183031394\n",
      "the 44 -th step is running\n",
      "the rmse of the 44 th step on train data is :106.52238644376824\n",
      "the 45 -th step is running\n",
      "the rmse of the 45 th step on train data is :106.4950164878475\n",
      "the 46 -th step is running\n",
      "the rmse of the 46 th step on train data is :106.46396751826454\n",
      "the 47 -th step is running\n",
      "the rmse of the 47 th step on train data is :106.43840325091566\n",
      "the 48 -th step is running\n",
      "the rmse of the 48 th step on train data is :106.41196959682347\n",
      "the 49 -th step is running\n",
      "the rmse of the 49 th step on train data is :106.39115512035082\n"
     ]
    }
   ],
   "source": [
    "n_steps = 50\n",
    "gamma = 0.04\n",
    "Lambda = 0.15\n",
    "\n",
    "for step in range(n_steps):\n",
    "    print ('the' ,step,'-th step is running')\n",
    "    \n",
    "    rmse_sum = 0.0\n",
    "    \n",
    "    kk = np.random.permutation(n_samples)\n",
    "    for j in range(n_samples):\n",
    "        index = kk[j]\n",
    "        \n",
    "        uid = uids[index]\n",
    "        iid = i_ids[index]\n",
    "        \n",
    "        eui = R[uid,iid] - pred_SVD(uid,iid)\n",
    "        \n",
    "        rmse_sum += eui**2\n",
    "        \n",
    "        bu[uid] += gamma*(eui - Lambda*bu[uid])\n",
    "        bi[iid] += gamma*(eui - Lambda*bi[iid])\n",
    "        \n",
    "        for k in range(K):\n",
    "            temp = P[uid,k] + gamma * eui * Q[k,iid] - Lambda * P[uid,k]\n",
    "            \n",
    "            Q[k,iid] += gamma * eui * P[uid,k] - Lambda * Q[k,iid]\n",
    "            P[uid,k] = temp\n",
    "    gamma = gamma * 0.93\n",
    "    print(\"the rmse of the {} th step on train data is :{}\".format(step,rmse_sum))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "cur_user = '5a905f000fc1ff3df7ca807d57edb608863db05d'\n",
    "cur_user_id = user_index[cur_user]\n",
    "cur_user_items = user_items[cur_user_id]\n",
    "\n",
    "sim_accumulate = 0.0\n",
    "rat_acc = 0.0\n",
    "\n",
    "user_items_scores = np.zeros(n_items)\n",
    "\n",
    "for i in range(n_items):\n",
    "    if i not in cur_user_items:\n",
    "        user_items_scores[i] = pred_SVD(cur_user_id,i)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 0.02355002,  0.0152817 ,  0.01445729,  0.00658585,  0.01266469,\n",
       "        0.00814827,  0.00769192,  0.01423053,  0.0134167 ,  0.01340101,\n",
       "        0.01200136,  0.0109209 ,  0.00993993,  0.00591263,  0.00498011,\n",
       "        0.        ,  0.00521831,  0.00677079,  0.01239203,  0.0034016 ,\n",
       "        0.00512964,  0.01098017,  0.00525378,  0.01045608,  0.00914918,\n",
       "        0.00989675,  0.        ,  0.00647652,  0.00440607,  0.00522627,\n",
       "        0.00677644,  0.00869304,  0.        ,  0.01029462,  0.01271659,\n",
       "        0.0042568 ,  0.        ,  0.00452141,  0.00501497,  0.        ,\n",
       "        0.00859238,  0.00394238,  0.00475615,  0.01324552,  0.01106667,\n",
       "        0.01069837,  0.00902776,  0.00431953,  0.00752002,  0.01114762,\n",
       "        0.00730603,  0.00419235,  0.00351614,  0.00457757,  0.00923821,\n",
       "        0.00617617,  0.00943535,  0.00475009,  0.00533175,  0.01123644,\n",
       "        0.00462411,  0.00798566,  0.0098    ,  0.00427123,  0.00397036,\n",
       "        0.00483735,  0.00484502,  0.00861956,  0.        ,  0.0041918 ,\n",
       "        0.00506825,  0.00393445,  0.00490344,  0.        ,  0.00405161,\n",
       "        0.00534337,  0.00476975,  0.00463237,  0.00637945,  0.0043653 ,\n",
       "        0.00800944,  0.00449455,  0.00398092,  0.00608709,  0.00394962,\n",
       "        0.        ,  0.00392155,  0.00533031,  0.00440609,  0.00383065,\n",
       "        0.01196079,  0.00414502,  0.00427366,  0.00966566,  0.0209807 ,\n",
       "        0.0059727 ,  0.00341703,  0.0048469 ,  0.00494325,  0.00445315,\n",
       "        0.00416296,  0.01913066,  0.00376796,  0.00383264,  0.        ,\n",
       "        0.0046353 ,  0.00343123,  0.00295303,  0.        ,  0.01184223,\n",
       "        0.00406289,  0.00391748,  0.01210592,  0.00430821,  0.00629358,\n",
       "        0.00380022,  0.00437611,  0.05692337,  0.0157132 ,  0.00672629,\n",
       "        0.00386035,  0.00992712,  0.01169776,  0.00280512,  0.00484383,\n",
       "        0.00474482,  0.00528105,  0.00646169,  0.00601902,  0.00380376,\n",
       "        0.00148016,  0.0113861 ,  0.00585457,  0.00409338,  0.00172981,\n",
       "        0.01857175,  0.01355028,  0.00386431,  0.00755343,  0.00564741,\n",
       "        0.00373375,  0.00623491,  0.00365936,  0.00379263,  0.00352395,\n",
       "        0.00428975,  0.00838553,  0.00536065,  0.00939184,  0.00436374,\n",
       "        0.01012786,  0.0095408 ,  0.01163625,  0.00398665,  0.00604276,\n",
       "        0.0145234 ,  0.0090091 ,  0.00518089,  0.00488809,  0.00405372,\n",
       "        0.00478088,  0.00429373,  0.00510358,  0.00340313,  0.00590082,\n",
       "        0.00364433,  0.00362368,  0.00404549,  0.01522285,  0.01419058,\n",
       "        0.00437208,  0.01357062,  0.00472243,  0.00500259,  0.        ,\n",
       "        0.01124374,  0.00337841,  0.00403232,  0.014415  ,  0.00417814,\n",
       "        0.00430231,  0.00459764,  0.0070968 ,  0.00996629,  0.00538584,\n",
       "        0.00318704,  0.00548759,  0.0049454 ,  0.01080119,  0.        ,\n",
       "        0.01698672,  0.        ,  0.00311748,  0.00910758,  0.00509483,\n",
       "        0.01568333,  0.00235163,  0.00325654,  0.00361698,  0.00856752,\n",
       "        0.00699486,  0.        ,  0.00574161,  0.0039882 ,  0.00441704,\n",
       "        0.00506839,  0.00451655,  0.00309965,  0.00460648,  0.0118989 ,\n",
       "        0.00571657,  0.02056849,  0.01224336,  0.01993696,  0.02894441,\n",
       "        0.01043138,  0.01447198,  0.00540548,  0.00950304,  0.00482549,\n",
       "        0.01124081,  0.01261951,  0.00380189,  0.0036177 ,  0.00446242,\n",
       "        0.00562416,  0.00975645,  0.00512481,  0.00948235,  0.00601219,\n",
       "        0.00370118,  0.01015868,  0.00457925,  0.00309758,  0.01270015,\n",
       "        0.01472148,  0.01267469,  0.        ,  0.01083826,  0.00799182,\n",
       "        0.01702846,  0.00371539,  0.00994177,  0.00455433,  0.01229934,\n",
       "        0.00931676,  0.00721081,  0.00341548,  0.00349255,  0.00513189,\n",
       "        0.0059846 ,  0.00369582,  0.01225584,  0.00563365,  0.00487203,\n",
       "        0.        ,  0.00262331,  0.00379297,  0.01408855,  0.00537419,\n",
       "        0.01203543,  0.00444066,  0.00795361,  0.00473051,  0.00731285,\n",
       "        0.00605097,  0.01306074,  0.00581008,  0.00723537,  0.02172907,\n",
       "        0.01930978,  0.00532513,  0.01715649,  0.00753202,  0.01153205,\n",
       "        0.00739977,  0.00249353,  0.00481103,  0.00252964,  0.00485781,\n",
       "        0.00733325,  0.01005309,  0.05850185,  0.00230277,  0.00946746,\n",
       "        0.00734912,  0.00748888,  0.01478865,  0.00422604,  0.00419601,\n",
       "        0.00326517,  0.01054724,  0.01003772,  0.00379355,  0.00531177,\n",
       "        0.02543477,  0.00939999,  0.00379722,  0.00770133,  0.01441693,\n",
       "        0.00308001,  0.01493619,  0.00363991,  0.00434655,  0.01234402,\n",
       "        0.00548365,  0.01044025,  0.00507633,  0.00206788,  0.01188283,\n",
       "        0.003342  ,  0.0262648 ,  0.00642988,  0.0303075 ,  0.00481068,\n",
       "        0.00926784,  0.00586378,  0.00548605,  0.0082487 ,  0.00427429,\n",
       "        0.00836839,  0.0064715 ,  0.        ,  0.01035881,  0.01002035,\n",
       "        0.00970688,  0.00624173,  0.00668573,  0.00630366,  0.00380996,\n",
       "        0.00574159,  0.00573775,  0.00420886,  0.0042126 ,  0.03351097,\n",
       "        0.        ,  0.00579427,  0.00546216,  0.00857314,  0.00337799,\n",
       "        0.00492368,  0.0040927 ,  0.00953852,  0.03225039,  0.00505961,\n",
       "        0.00456175,  0.0036249 ,  0.00529592,  0.00935315,  0.01334109,\n",
       "        0.00694412,  0.00570404,  0.0120799 ,  0.01109568,  0.00511078,\n",
       "        0.        ,  0.01127402,  0.00965962,  0.01054584,  0.00367323,\n",
       "        0.00611794,  0.00701623,  0.00569489,  0.00342531,  0.00541392,\n",
       "        0.00773262,  0.01546628,  0.0039505 ,  0.00782603,  0.00388016,\n",
       "        0.00413063,  0.01489587,  0.01078985,  0.00641847,  0.00497968,\n",
       "        0.        ,  0.00637286,  0.00944395,  0.02913353,  0.00367906,\n",
       "        0.0139239 ,  0.00935727,  0.00410512,  0.0145414 ,  0.00476891,\n",
       "        0.00573364,  0.00455325,  0.00558148,  0.01265293,  0.00427219,\n",
       "        0.00531368,  0.00671204,  0.01113323,  0.00725571,  0.00449444,\n",
       "        0.00722288,  0.00736677,  0.00394417,  0.01435046,  0.01963171,\n",
       "        0.        ,  0.00440213,  0.00422621,  0.00338882,  0.01040692,\n",
       "        0.00683251,  0.00523055,  0.        ,  0.00400958,  0.00546203,\n",
       "        0.0069294 ,  0.00566496,  0.00179107,  0.00342886,  0.00491723,\n",
       "        0.00621868,  0.02058669,  0.00528423,  0.00813824,  0.00026866,\n",
       "        0.00496937,  0.00727936,  0.00542587,  0.        ,  0.00913381,\n",
       "        0.0060355 ,  0.00540778,  0.00874915,  0.00856303,  0.01033126,\n",
       "        0.0030581 ,  0.00339645,  0.00440666,  0.01093716,  0.0133055 ,\n",
       "        0.00695239,  0.0046018 ,  0.01362809,  0.00535991,  0.00620825,\n",
       "        0.00961515,  0.01195655,  0.00568413,  0.00362357,  0.0051797 ,\n",
       "        0.0083747 ,  0.00357269,  0.00496711,  0.00267454,  0.00380906,\n",
       "        0.00664005,  0.        ,  0.00255083,  0.00647354,  0.00271254,\n",
       "        0.01345623,  0.00503534,  0.00723866,  0.00752341,  0.00406474,\n",
       "        0.01921557,  0.00593573,  0.01119877,  0.02025709,  0.00965747,\n",
       "        0.0042653 ,  0.00907217,  0.00687532,  0.00588307,  0.00691588,\n",
       "        0.00267036,  0.00388943,  0.00505028,  0.        ,  0.004946  ,\n",
       "        0.00449069,  0.        ,  0.06747749,  0.00524341,  0.00399717,\n",
       "        0.00519774,  0.0047912 ,  0.00645528,  0.006635  ,  0.00474186,\n",
       "        0.00599906,  0.01886519,  0.00753216,  0.00743881,  0.00406365,\n",
       "        0.00443161,  0.00368438,  0.00863769,  0.00471892,  0.0060135 ,\n",
       "        0.00689354,  0.00673693,  0.01011028,  0.01609964,  0.03724736,\n",
       "        0.00334753,  0.01534235,  0.01044255,  0.00550538,  0.0050809 ,\n",
       "        0.00693741,  0.00446442,  0.00782085,  0.00533643,  0.00250192,\n",
       "        0.00432467,  0.01201179,  0.01213054,  0.00232   ,  0.0069896 ,\n",
       "        0.00550213,  0.        ,  0.0033759 ,  0.01165433,  0.        ,\n",
       "        0.00257051,  0.        ,  0.00496027,  0.        ,  0.00439085,\n",
       "        0.01148635,  0.        ,  0.04853387,  0.00303546,  0.00528531,\n",
       "        0.00578202, -0.00010914,  0.00849103,  0.00406615,  0.01505803,\n",
       "        0.006618  ,  0.01037729,  0.00802417,  0.00781111,  0.00756782,\n",
       "        0.01212822,  0.0009748 ,  0.00691781,  0.00434612,  0.02060179,\n",
       "        0.00516537,  0.00458107,  0.        ,  0.00673929,  0.01976874,\n",
       "        0.02486703,  0.00511223,  0.0173251 ,  0.00834385,  0.01053881,\n",
       "        0.00659307,  0.00352218,  0.01069855,  0.00369208,  0.00318793,\n",
       "        0.0094194 ,  0.00640869,  0.00971632,  0.01343301,  0.00542258,\n",
       "        0.00451055,  0.        ,  0.00611381,  0.00496059,  0.00358584,\n",
       "        0.00361823,  0.01130266,  0.00292963,  0.01116084,  0.00627415,\n",
       "        0.00432013,  0.        ,  0.00472554,  0.01371039,  0.00875406,\n",
       "        0.        ,  0.00516535,  0.00803513,  0.00559416,  0.00797793,\n",
       "        0.00700405,  0.0046406 ,  0.00489558,  0.        ,  0.00391997,\n",
       "        0.00347333,  0.00625311,  0.01134645,  0.02988117,  0.00422471,\n",
       "        0.00501277,  0.00455149,  0.0043564 ,  0.01971753,  0.00643652,\n",
       "        0.00287208,  0.01368265,  0.00365248,  0.00595104,  0.00535703,\n",
       "        0.00459367,  0.00907245,  0.        ,  0.01024209,  0.00353116,\n",
       "        0.01056041,  0.00812661,  0.00065171,  0.00895999,  0.00445145,\n",
       "        0.01049938,  0.01687126,  0.01233735,  0.01016358,  0.00641058,\n",
       "        0.02538514,  0.00952133,  0.003346  ,  0.00652864,  0.00965807,\n",
       "        0.00783207,  0.01116987,  0.01022835,  0.00462774,  0.02811026,\n",
       "        0.02556838,  0.0061518 ,  0.00331864,  0.00468517,  0.0037065 ,\n",
       "        0.0050093 ,  0.00522442,  0.00688432,  0.02552841,  0.00938374,\n",
       "        0.01245291,  0.00594552,  0.00736034,  0.00353626,  0.02116719,\n",
       "        0.0074491 ,  0.00721373,  0.01624922,  0.00450359,  0.00491205,\n",
       "        0.00544996,  0.002915  ,  0.01453985,  0.00852732,  0.00578592,\n",
       "        0.00861732,  0.00360964,  0.05052117,  0.00980622,  0.00921433,\n",
       "        0.00415872,  0.00347454,  0.0029498 ,  0.00182521,  0.00498505,\n",
       "        0.0040856 ,  0.00473975,  0.00463729,  0.01306369,  0.0071914 ,\n",
       "        0.01260349,  0.00552487,  0.00383843,  0.00446819,  0.00862143,\n",
       "        0.00343562,  0.00433815,  0.        ,  0.01089703,  0.00786534,\n",
       "        0.00294479,  0.00332262,  0.00468192,  0.01260369,  0.00424081,\n",
       "        0.0054631 ,  0.00501781,  0.00244378,  0.        ,  0.00654047,\n",
       "        0.00886781,  0.00871711,  0.02274167,  0.00826679,  0.00332921,\n",
       "        0.00620507,  0.00326175,  0.00792085,  0.        ,  0.00925557,\n",
       "        0.00231347,  0.00307785,  0.00452064,  0.00411446,  0.00972047,\n",
       "        0.00475548,  0.01154087,  0.00338752,  0.00775376,  0.00495732,\n",
       "        0.00554436,  0.0047997 ,  0.00584974,  0.00619937,  0.00441299,\n",
       "        0.00371508,  0.00925091,  0.01997971,  0.01247007,  0.00542428,\n",
       "        0.00468915,  0.00582441,  0.0107219 ,  0.00679002,  0.01122372,\n",
       "        0.00411005,  0.02528585,  0.        ,  0.00840681,  0.01065837,\n",
       "        0.00688084,  0.00628287,  0.00641278,  0.00397073,  0.00846605,\n",
       "        0.00269105,  0.00721948,  0.00294335,  0.00320803,  0.01641648,\n",
       "        0.00688689,  0.00532652,  0.00432716,  0.00522387,  0.00527685,\n",
       "        0.00209323,  0.00519   ,  0.        ,  0.        ,  0.00465034,\n",
       "        0.00716095,  0.00469827,  0.00416508,  0.01128119,  0.01105832,\n",
       "        0.00359764,  0.0047526 ,  0.0057544 ,  0.00320332,  0.00369478,\n",
       "        0.0030317 ,  0.01537785,  0.01207484,  0.        ,  0.00841648,\n",
       "        0.00855584,  0.00817472,  0.00623937,  0.00366661,  0.00550573,\n",
       "        0.00397518,  0.0069217 ,  0.00592971,  0.01209353,  0.00700371,\n",
       "        0.00094686,  0.00546461,  0.00454508,  0.004241  ,  0.00331589,\n",
       "        0.00518342,  0.01047587,  0.00365875,  0.00386772,  0.01070377,\n",
       "        0.01290504,  0.00919233,  0.00749878,  0.01334808,  0.00712959,\n",
       "        0.00466694,  0.00330654,  0.00317611,  0.00536666,  0.00826319,\n",
       "        0.01335649,  0.00719363,  0.00393606,  0.00892285,  0.03124636,\n",
       "        0.00517014,  0.00745119,  0.00502452,  0.00303785,  0.00648504,\n",
       "        0.00288857,  0.0072946 ,  0.00779905,  0.00977065,  0.00357145,\n",
       "        0.00514582,  0.00813594,  0.02201755,  0.00603432,  0.01128767,\n",
       "        0.0201973 ,  0.00444244,  0.00390293,  0.        ,  0.00534038,\n",
       "        0.00720911,  0.01293803,  0.00344551,  0.00749834,  0.02141525,\n",
       "        0.00668741,  0.        ,  0.02246302,  0.00266394,  0.0058103 ,\n",
       "        0.00881635,  0.00659736,  0.00405531,  0.00993555,  0.        ,\n",
       "        0.00834016,  0.01028152,  0.01244252,  0.00313299,  0.01222149,\n",
       "        0.00604315,  0.0096946 ,  0.0072539 ,  0.00168325,  0.00981334,\n",
       "        0.0081891 ,  0.00725718,  0.00672096,  0.00463226,  0.00456554,\n",
       "        0.        ,  0.00625362,  0.04249133,  0.00312071,  0.00433946,\n",
       "        0.01443601,  0.        ,  0.00618802,  0.00471045,  0.00605879,\n",
       "        0.00313193,  0.00343343,  0.00892562,  0.02488448,  0.00613891,\n",
       "        0.00314575,  0.00723242,  0.00093556,  0.00694285,  0.00255144,\n",
       "        0.00488836,  0.00969197,  0.00532476,  0.00578296,  0.00798955,\n",
       "        0.00417488,  0.00387746,  0.        ,  0.00306923,  0.00494712,\n",
       "        0.0037842 ,  0.00343357,  0.00882877,  0.00251117,  0.00527333,\n",
       "        0.01137964,  0.00302497,  0.00537267,  0.01474355,  0.01509793,\n",
       "        0.00432585,  0.00458093,  0.01055039,  0.00755516,  0.        ,\n",
       "        0.00683601,  0.00658251,  0.00169768,  0.01219459,  0.00510318,\n",
       "        0.00814265,  0.0043362 ,  0.00567357,  0.00312893,  0.0089682 ,\n",
       "        0.00515925,  0.0130849 ,  0.00435516,  0.02471145,  0.01457095,\n",
       "        0.0204665 ,  0.00363148,  0.00529396,  0.        ,  0.00337859,\n",
       "        0.00499867,  0.00672264,  0.        ,  0.00174231,  0.00584949,\n",
       "        0.01059273,  0.        ,  0.        ,  0.00534663,  0.01033924,\n",
       "        0.00955327,  0.00451985,  0.00823683,  0.01495267,  0.0152745 ,\n",
       "        0.00328462,  0.00560363,  0.00982654,  0.00767109,  0.02880099,\n",
       "        0.00343759,  0.01220239,  0.00606847,  0.00740372,  0.00784356,\n",
       "        0.00428597,  0.00010608,  0.00464846,  0.00484403,  0.01258951,\n",
       "        0.00356507,  0.00587188,  0.00355667,  0.0069628 ,  0.00391508,\n",
       "        0.01176556,  0.02343204,  0.00782991,  0.00546898,  0.        ,\n",
       "        0.00840314,  0.00293006,  0.01182496,  0.01002053,  0.01751321,\n",
       "        0.00463031,  0.00571207,  0.00310377,  0.00700697,  0.00472076,\n",
       "        0.00387386,  0.01285945,  0.00493751,  0.00780591,  0.01034514,\n",
       "        0.00248944,  0.006945  ,  0.00559968,  0.00403778,  0.01606404,\n",
       "       -0.00050943,  0.0049751 ,  0.00477154,  0.00866457,  0.0126511 ,\n",
       "        0.00276975,  0.0029807 ,  0.00779966,  0.00685571,  0.00477558,\n",
       "        0.0047572 ,  0.00894533,  0.01157632,  0.01572932,  0.01261048,\n",
       "        0.00830882,  0.00392767,  0.00384468,  0.00291379,  0.01043006,\n",
       "        0.00590892,  0.02237215,  0.01753305,  0.00293119,  0.01018272])"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "user_items_scores"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "sort_index = sorted(((e,i) for i,e in enumerate(list(user_items_scores))),reverse = True)\n",
    "\n",
    "columns = ['user_id','item','score','rank']\n",
    "\n",
    "df = pd.DataFrame(columns = columns)\n",
    "\n",
    "rank = 1\n",
    "for i in range(0,len(sort_index)):\n",
    "    cur_item_index = sort_index[i][1]\n",
    "    cur_item = list(item_index.keys())[list (item_index.values()).index(cur_item_index)]\n",
    "    \n",
    "    if ~np.isnan(sort_index[i][0]) and cur_item_index not in cur_user_items and rank <= 20:\n",
    "        df.loc[len(df)] = [cur_user,cur_item,sort_index[i][0],rank]\n",
    "        rank = rank + 1"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
